import { request } from '@/app/http'
import { toRangeParam } from 'app/converter'

const PATH = '/dept'

function toSaveParam(data) {
  const res = { ...data }
  delete res.isTop
  const { isTop } = data
  isTop && (res.pids = [])
  return res
}

export async function searchDept(params) {
  return request({
    url: PATH,
    method: 'get',
    params: { ...toRangeParam(params) }
  })
}

export function addDept(data) {
  return request({
    url: PATH,
    method: 'post',
    data: toSaveParam(data)
  })
}

export function updateDept(data) {
  return request({
    url: PATH,
    method: 'put',
    data: toSaveParam(data)
  })
}

export function removeDept(data) {
  return request({
    url: PATH,
    method: 'delete',
    data,
    showLoading: true,
    showMsg: true
  })
}

export function downloadDept(params) {
  return request({
    url: PATH + '/download',
    method: 'get',
    params,
    responseType: 'blob',
    showLoading: true,
    showMsg: true
  })
}
